//
//  ViewController.swift
//  baidumap
//pfSvkAevxATXEBFaSL7GxXye
//  Created by admin on 14/12/3.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//

import UIKit

class ViewController: UIViewController, BMKMapViewDelegate, BMKLocationServiceDelegate {

    var _mapView: BMKMapView?
    var _locationService: BMKLocationService?
    var _dataManager: XZDataManager?
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        _mapView?.delegate = self
        _locationService?.delegate = self
        _locationService?.startUserLocationService()
        _mapView?.showsUserLocation = true
        _mapView?.userTrackingMode = BMKUserTrackingModeFollow
      //  _mapView?.showsUserLocation = true
        var pointAnnotation = CustomPointAnnotation()
        //println("pointAnnotation.pointCalloutInfo=\(pointAnnotation.pointCalloutInfo)")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        _mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: 320, height: 460))
        self.view = _mapView!
        
        _locationService = BMKLocationService()
    }

    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        _mapView?.delegate = nil
        _locationService?.delegate = nil
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    
    //MARK: - BMKLocationServiceDelegate
    /**
    *在地图View将要启动定位时，会调用此函数
    *@param mapView 地图View
    */
    func willStartLocatingUser(){
        println("willStartLocatingUser")
    }
    /**
    *在地图View停止定位后，会调用此函数
    *@param mapView 地图View
    */
    func didStopLocatingUser(){
        println("didStopLocatingUser")
    }
    
    /**
    *用户方向更新后，会调用此函数
    *@param userLocation 新的用户位置
    */
    
    func didUpdateUserHeading(userLocation: BMKUserLocation!) {
        _mapView?.updateLocationData(userLocation)
    }
    /**
    *用户位置更新后，会调用此函数
    *@param userLocation 新的用户位置
    */
    func didUpdateUserLocation(userLocation: BMKUserLocation!) {
//        println("didUpdateUserLocation---location=\(userLocation.location)")
//        println("didUpdateUserLocation---heading=\(userLocation.heading)")
        _mapView?.updateLocationData(userLocation)
        setMapRegionWithCoordinate(userLocation.location.coordinate)
    }
    /**
    *定位失败后，会调用此函数
    *@param error 错误号
    */
    func didFailToLocateUserWithError(error: NSError){
        println("didFailToLocateUserWithError--error=\(error)")
    }
    
    
    
    func setMapRegionWithCoordinate(coordinate: CLLocationCoordinate2D){
        var region: BMKCoordinateRegion = BMKCoordinateRegionMake(coordinate, BMKCoordinateSpanMake(0.05, 0.05))
        _mapView?.setRegion(region, animated: true)
        //给指定位置添加标注
        var item: BMKPointAnnotation = BMKPointAnnotation()
        item.coordinate = coordinate
        item.title = "公司"
        item.subtitle = "超级无敌"
        
        _mapView?.addAnnotation(item)
         _mapView?.showsUserLocation = false
    }
    
    //MARK: - BMKMapViewDelegate
    /**
    *当mapView新添加annotation views时，调用此接口
    *@param mapView 地图View
    *@param views 新添加的annotation views
    */
  //  - (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
    
    func mapView(mapView: BMKMapView!, didAddAnnotationViews views: [AnyObject]!) {
        println("didAddAnnotationViews")
    }
    
    
    
    func mapView(mapView: BMKMapView!, viewForAnnotation annotation: BMKAnnotation!) -> BMKAnnotationView! {
        println("viewForAnnotation")
        var annotationViewID = "renameMark"
        var newAnnotation: BMKPinAnnotationView = BMKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationViewID)

        return newAnnotation
    }
}

